/*
 * @Autor: liangbo
 * @Description: 后端服务API入口
 * @Version: 1.0
 * @Date: 2020-05-10 15:24:51
 */
const express = require('express');
const router = express.Router();
const glob = require('glob');
const path = require('path')
const files = glob.sync( path.join(__dirname, "**/*.js") )
const WBMSFilter = require('../comm/middleware').WBMSFilter

for( let file of files ){
  file = file.replace(/\//g, path.sep).replace(__dirname + path.sep, '')
  const file_name = file.substr(0, file.length - 3);
  const file_entity = require(path.join(__dirname, file));
  let ischild = file_name.split(path.sep)
  if (file_name == 'index') continue
  
  if( ischild.length > 1 ){
    //let lastfilename = ischild.slice(-1)
    let childpath = []
    for( let c of ischild ){
      childpath.push(c)
    }

    if( childpath[0] == 'wbms'){
      router.use(`/api/${childpath.join('/')}`, WBMSFilter)
      router.use(`/api/${childpath.join('/')}`, file_entity)
    }else{
      router.use(`/api/${childpath.join('/')}`, file_entity)
    }
  }else{
    router.use(`/api/${file_name}`, file_entity)
  }
}

process.on('unhandledRejection', (reason, p) => {
  console.log('Unhandled Rejection at:', p, 'reason:', reason);
});

// comm.redis.set

module.exports = router